<template>
    <el-table
        id="fixreplys-table-fixreplys"
        ref="tableRef"
        :data="tableData"
        v-bind="tableDefaultAttribute"
        row-key="id"
        :max-height="tableHeight"
        table-layout="auto"
        @cell-dblclick="onFixList"
    >
        <template #empty>
            <span>无数据</span>
        </template>
        <el-table-column type="selection" width="55" :align="'center'" />
        <template v-for="col in tableColumns" :key="col.prop">
            <el-table-column
                v-if="!col.isHidden"
                :align="col.align"
                :prop="col.prop"
                :label="col.label"
                :formatter="col.formatter"
                :type="col.type"
                :fixed="col.fixed"
                :width="col.width"
            >
            </el-table-column>
        </template>
    </el-table>
    <el-button-group class="bg">
        <el-space>
            <SearchBtn @click="onQuery"></SearchBtn>
            <RefreshBtn @click="onRefresh"></RefreshBtn>
            
            <el-button
                class="btn"
                type="primary"
                :icon="CircleCheck"
                @click="onFixReply"
                >修箱报价批复</el-button
            >            
        </el-space>
    </el-button-group>
    <div class="page">
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[50, 100, 150, 200]"
            layout="sizes, prev, pager, next, total"
            :total="totalnum"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <!-- <Page :onRefresh="onRefresh"></Page> -->
    </div>
    <el-dialog
        v-model="dialogFixVisible"
        title="箱修理明细"
        draggable
        destroy-on-close
        width="80%"
    >
        <FixList
            :cntr_no="paramCntrNo"
            :caller="'FixReplys'"
            @yh-closedialog="onCloseDialog"
        ></FixList>
    </el-dialog>
    <el-dialog
        v-model="dialogFixReplyVisible"
        title="修箱报价批复"
        draggable
        destroy-on-close
        width="80%"
    >
        <CntrReply
            :ids="paramCntrNos"
            @close="onCloseDialog"
        ></CntrReply>
    </el-dialog>    
    <el-dialog
        v-model="dialogQueryVisible"
        title="修箱报价查询"
        draggable
        destroy-on-close
    >
        <QueryForm
            :queryItems="queryItems"
            :tableID="tableRef!.$el.id"
            @yh-closedialog="onCloseDialog"
        ></QueryForm>
    </el-dialog>
</template>
<script setup lang="ts" name="FixReplys">
import { tableMaxHeightAdjust } from '@/common/const';
import {
    formatDateTime,
    pickQueryFromTable,
    transFormatter,
} from '@/common/tools';
import { QueryItem, TableColumn } from '@/common/types/app';
import { VCntrFixReplyModel } from '@/common/types/container';
import {
    tableColumnDefault,
    tableDefaultAttribute,
    useTableInfo,
} from '@/hooks/useTable';
import { ElMessage, TableInstance } from 'element-plus';
import { Scissor, CircleCheck } from '@element-plus/icons-vue';
import { ref, reactive, onMounted } from 'vue';
import { useAppStore } from '@/store/app';
import { storeToRefs } from 'pinia';
import { Request } from '@/app/request';
import { isEmpty } from 'lodash';
import SearchBtn from '@/components/button/SearchBtn.vue';
import RefreshBtn from '@/components/button/RefreshBtn.vue';
import FixList from '@/views/cfs/repair/Fixlist.vue';
import CntrReply from './CntrReply.vue';
const tableRef = ref<TableInstance>();
const tableData = ref<Array<VCntrFixReplyModel>>([]);
const tableHeight = ref(600);
const paramCntrNo = ref(0);
const paramCntrNos = ref<Array<number>>([]);
const { cntrOwner, replyId, cyCfs } = storeToRefs(useAppStore());

const onFormatter = (_row: any, column: any, cellValue: any) => {
    let data: { value: string | number; label: string }[];
    if (column['property'] === 'cntr_owner') {
        data = cntrOwner.value;
    } else if (column['property'] === 'repair_reply_id') {
        data = replyId.value;
    } else if (column['property'] === 'cfs_cod') {
        data = cyCfs.value;
    } else {
        return cellValue;
    }
    return transFormatter(cellValue, data);
};
const tableColumns = reactive<TableColumn[]>([
    {
        ...tableColumnDefault,
        order: 1,
        prop: 'cntr',
        label: '箱号',
        formFieldItemType: 'input',
        width: 200,
    },
    {
        ...tableColumnDefault,
        order: 2,
        prop: 'repair_tim',
        label: '报价时间',
        formatter: formatDateTime,
        formFieldItemType: 'datetime',
        width: 250,
    },
    {
        ...tableColumnDefault,
        order: 5,
        prop: 'cntr_siz_cod',
        label: '箱尺寸',
        sortable: 'custom',
        width: 80,
    },
    {
        ...tableColumnDefault,
        order: 6,
        prop: 'cntr_typ_cod',
        label: '箱型',
        sortable: 'custom',
        width: 80,
    },
    {
        ...tableColumnDefault,
        order: 6,
        prop: 'cfs_cod',
        label: '站点',
        formatter: onFormatter,
        sortable: 'custom',
        formFieldItemType: 'single',
        optionalValues: cyCfs.value,
        width: 200,
    },
    {
        ...tableColumnDefault,
        order: 15,
        prop: 'cntr_owner',
        label: '箱主',
        sortable: 'custom',
        formatter: onFormatter,
        formFieldItemType: 'single',
        optionalValues: cntrOwner.value,
        width: 200,
    },
    {
        ...tableColumnDefault,
        order: 6,
        prop: 'repair_reply_id',
        label: '报价状态',
        sortable: 'custom',
        formatter: onFormatter,
        formFieldItemType: 'single',
        optionalValues: replyId.value,
        width: 100,
    },
    {
        ...tableColumnDefault,
        order: 6,
        prop: 'fix_amount',
        label: '修箱费',
        sortable: 'custom',
        width: 150,
    },

]);
const dialogQueryVisible = ref(false);
const dialogFixVisible = ref(false);
const dialogFixReplyVisible = ref(false);
let queryWhere = {};
const queryItems: Array<QueryItem> = pickQueryFromTable(tableColumns);
const onRefresh = async () => {
    const req = new Request('修箱报价提交查询', {
        skip: (currentPage.value - 1) * pageSize.value,
        take: pageSize.value,
        order: orders,
        ...queryWhere,
    });
    const r = await req.post();

    totalnum.value = r.ext;
    tableData.value.length = 0;

    if (!isEmpty(r.obj)) {
        tableData.value = r.obj;
    }
};
const onQuery = () => {
    dialogQueryVisible.value = true;
};
const {
    currentPage,
    orders,
    pageSize,
    totalnum,
    handleSizeChange,
    handleCurrentChange,
    handleSortChange,
} = useTableInfo(onRefresh);
onMounted(() => {
    tableHeight.value = window.innerHeight - tableMaxHeightAdjust;
});
const onFixList = (row: any) => {
    /* const rows: Array<VCntrFixReplyModel> = tableRef.value?.getSelectionRows();
    if (rows.length !== 1) {
        ElMessage.info({
            message: '请选择一个记录',
        });
        return;
    } */ 
    paramCntrNo.value = row.cntr_no
    dialogFixVisible.value = true;
    
    
}
const onFixReply = () => {
    const rows: Array<VCntrFixReplyModel> = tableRef.value?.getSelectionRows() || [];
    if (rows.length === 0) {
        ElMessage.info({
            message: '请选择记录',
        });
        return;
    }
    paramCntrNos.value.length = 0;
    for (let index = 0; index < rows.length; index++) {
        const element = rows[index];
        paramCntrNos.value.push(element.cntr_no as number);
    }
    dialogFixReplyVisible.value = true;
}
const onCloseDialog = async (arg?: any) => {
    if (dialogFixVisible.value) {
        dialogFixVisible.value = false;
    }
    if (dialogFixReplyVisible.value) {
        dialogFixReplyVisible.value = false;
    }
    if (dialogQueryVisible.value) {
        // 查询
        currentPage.value = 1;
        queryWhere = arg;
        onRefresh();

        dialogQueryVisible.value = false;
    }
};
</script>
<style lang="scss" scoped>
.page {
    float: right;
    margin-top: 10px;
}
.bg {
    margin-top: 10px;

    .btn {
        margin-right: 10px;
    }
}
</style>
